import { TRPCClientError, createTRPCProxyClient, httpBatchLink, loggerLink } from "@trpc/client";
import type { AppRouter } from "@trpc/etype/Routes/AppRouter";

let url = "http://localhost:3000/trpc";

export let trpc = createTRPCProxyClient<AppRouter>({
    links: [
        loggerLink({
            enabled(opts) {
                if (opts.direction === "down" && opts.result instanceof TRPCClientError)
                {
                    console.error(`错误:` + opts.result.message);
                }
                return false;
            }
        }),
        httpBatchLink({
            url: url,
            fetch(url, options) {
                return fetch(url, {
                    ...options,
                    mode: "cors",
                });
            }
        }),
    ],
    transformer: undefined,
});
